﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using l1cs.server.model.Instance;
using l1cs.server.serverpackets;

namespace l1cs.server.model
{
    public class L1Trade
    {
        private static L1Trade _instance;

        public L1Trade() { }

        public static L1Trade getInstance()
        {
            if ( _instance == null )
            {
                _instance = new L1Trade();
            }
            return _instance;
        }

        public void TradeAddItem( L1PcInstance player, int itemid, int itemcount )
        {
            L1PcInstance trading_partner = (L1PcInstance)L1World.getInstance()
                    .findObject( player.getTradeID() );
            L1ItemInstance l1iteminstance = player.getInventory().getItem( itemid );
            if ( l1iteminstance != null && trading_partner != null )
            {
                if ( !l1iteminstance.isEquipped() )
                {
                    if ( l1iteminstance.getCount() < itemcount || 0 > itemcount )
                    {
                        player.sendPackets( new S_TradeStatus( 1 ) );
                        trading_partner.sendPackets( new S_TradeStatus( 1 ) );
                        player.setTradeOk( false );
                        trading_partner.setTradeOk( false );
                        player.setTradeID( 0 );
                        trading_partner.setTradeID( 0 );
                        return;
                    }
                    player.getInventory().tradeItem( l1iteminstance, itemcount,
                            player.getTradeWindowInventory() );
                    player.sendPackets( new S_TradeAddItem( l1iteminstance,
                            itemcount, 0 ) );
                    trading_partner.sendPackets( new S_TradeAddItem( l1iteminstance,
                            itemcount, 1 ) );
                }
            }
        }

        public void TradeOK( L1PcInstance player )
        {
            int cnt;
            L1PcInstance trading_partner = (L1PcInstance)L1World.getInstance()
                    .findObject( player.getTradeID() );
            if ( trading_partner != null )
            {
                object[] player_tradelist = player.getTradeWindowInventory().getItems();
                int player_tradecount = player.getTradeWindowInventory().getSize();

                object[] trading_partner_tradelist = trading_partner.getTradeWindowInventory().getItems();
                int trading_partner_tradecount = trading_partner.getTradeWindowInventory().getSize();

                for ( cnt = 0; cnt < player_tradecount; cnt++ )
                {
                    L1ItemInstance l1iteminstance1 = (L1ItemInstance)player_tradelist[0];
                    player.getTradeWindowInventory().tradeItem( l1iteminstance1,
                            l1iteminstance1.getCount(), trading_partner.getInventory() );
                }
                for ( cnt = 0; cnt < trading_partner_tradecount; cnt++ )
                {
                    L1ItemInstance l1iteminstance2 = (L1ItemInstance)trading_partner_tradelist[0];
                    trading_partner.getTradeWindowInventory().tradeItem(
                            l1iteminstance2, l1iteminstance2.getCount(), player.getInventory() );
                }

                player.sendPackets( new S_TradeStatus( 0 ) );
                trading_partner.sendPackets( new S_TradeStatus( 0 ) );
                player.setTradeOk( false );
                trading_partner.setTradeOk( false );
                player.setTradeID( 0 );
                trading_partner.setTradeID( 0 );
                player.turnOnOffLight();
                trading_partner.turnOnOffLight();
            }
        }

        public void TradeCancel( L1PcInstance player )
        {
            int cnt;
            L1PcInstance trading_partner = (L1PcInstance)L1World.getInstance()
                    .findObject( player.getTradeID() );
            if ( trading_partner != null )
            {
                object[] player_tradelist = player.getTradeWindowInventory().getItems();
                int player_tradecount = player.getTradeWindowInventory().getSize();

                object[] trading_partner_tradelist = trading_partner
                        .getTradeWindowInventory().getItems();
                int trading_partner_tradecount = trading_partner
                        .getTradeWindowInventory().getSize();

                for ( cnt = 0; cnt < player_tradecount; cnt++ )
                {
                    L1ItemInstance l1iteminstance1 = (L1ItemInstance)player_tradelist[0];
                    player.getTradeWindowInventory().tradeItem( l1iteminstance1,
                            l1iteminstance1.getCount(), player.getInventory() );
                }
                for ( cnt = 0; cnt < trading_partner_tradecount; cnt++ )
                {
                    L1ItemInstance l1iteminstance2 = (L1ItemInstance)trading_partner_tradelist[0];
                    trading_partner.getTradeWindowInventory().tradeItem(
                            l1iteminstance2, l1iteminstance2.getCount(),
                            trading_partner.getInventory() );
                }

                player.sendPackets( new S_TradeStatus( 1 ) );
                trading_partner.sendPackets( new S_TradeStatus( 1 ) );
                player.setTradeOk( false );
                trading_partner.setTradeOk( false );
                player.setTradeID( 0 );
                trading_partner.setTradeID( 0 );
            }
        }
    }
}
